import { Component, ElementRef, OnInit, Output, Input, ViewChild, AfterViewInit, EventEmitter } from '@angular/core';
import { HttpModule, Headers, RequestOptions, Http } from '@angular/http';
import { HttpClientModule, HttpClient, HttpErrorResponse } from '@angular/common/http';
import { FormsModule } from '@angular/forms';

import { Service } from '../../../server/server';
import { CudTableService } from '../../../public/serve/cud-table/cud-table.service';
import * as moment from 'moment';
import { addDatatype, roledatas } from '../../institution/institution-add-admin/institution-add-admin.component';
import { Data } from '@angular/router/src/config';
import { HintsService } from '../../../public/serve/hints/hints.service';
declare var $: any;



@Component({
  selector: 'app-management-update',
  templateUrl: './management-update.component.html'
})
export class ManagementUpdateComponent implements OnInit {

  constructor(
    private server: Service, 
    private httpc: HttpClient, 
    private http: Http, 
    private cudTableService: CudTableService,
    private hintsService :HintsService 
  ) { }

  ngOnInit() {

    this.createUserId = this.server.getuser();
    this.managementUpdateeventEmit = this.server.managementUpdateeventEmit.subscribe((value: any) => {
      this.userId = value;
      this.update = {
        userId: this.userId,
        createUserId: this.createUserId,
      }
      this.getDetailss();
      //console.log(this.userId)
    });

    //接受角色
    this.managementroleeeventEmit = this.server.managementroleeeventEmit.subscribe((value: any) => {
      this.roledata = value;
      //console.log(this.roledata)
    });
    //首次进入接受tree数据
    this.managementtreeidnameeventEmit = this.server.managementtreeidnameeventEmit.subscribe((value: any) => {
      this.superiorUpdate = value.institutionName;
      this.superiorUpdateId = value.institutionId;
    })

    this.maxDate = new Date();
  }

  //取消订阅
  public managementUpdateeventEmit: any;
  public managementroleeeventEmit: any;
  public managementtreeidnameeventEmit: any;
  ngOnDestroy() {
    if (this.managementUpdateeventEmit || this.managementroleeeventEmit || this.managementtreeidnameeventEmit) {
      this.managementUpdateeventEmit.unsubscribe();
      this.managementroleeeventEmit.unsubscribe();
      this.managementtreeidnameeventEmit.unsubscribe();
    }
  }

  public maxDate: Data;
  //接受角色
  public roledata: roledatas;

  @Input() superiorUpdate: any;

  @Input() superiorUpdateId: any;

  public getTabVal: any;

  public userId: any;

  public createUserId: any;

  public update: any;
  @Output()
  updateOutput: EventEmitter<any[]> = new EventEmitter();

  //填充
  getDetailss() {
    let url = this.server.baseUrl+'admin/pusers/getPusersInfo/' + this.userId + '';
    this.httpc.get<Response>(url).subscribe((data: any) => {
      //console.log(data.result)
      this.getTabVal = data.result;
      if (this.getTabVal.birthday) {
        this.getTabVal.birthday = new Date(this.getTabVal.birthday);
      }
      //console.log(this.getTabVal)
    })
  }

  //修改
  submitEdit(val) {
    //console.info(val)
    let url = this.server.baseUrl+'admin/pusers/modify';

    //console.log(data)

    //转换时间格式
    val.birthday = this.server.formatDate(val.birthday);
    val.status = JSON.stringify(Number(val.status));
    val.isActivate = JSON.stringify(Number(val.isActivate));
    val.isAutonym = JSON.stringify(Number(val.isAutonym));
    let formData = new FormData();
    for (let key in val) {
      formData.append(key, val[key] || "");
    }
    //formData.append("imageinfo", $('#picID2')[0].files[0] || '');
    let headers = new Headers({ 'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundaryhdymHRIkeSu' });
    let options = new RequestOptions({ headers: headers });
    headers.delete("Content-Type");
    this.http.post(url, formData, options).toPromise().then((res: any) => {
      res = res.json();
      this.hintsService.messages(res.message,res.result_code);
      if (res.result_code == '0000') {
        //console.log(res, this.server.getuserPortrait(), res.portrait, this.server.getuserName(), -res.displayName)
        //头像
        if (res.portrait && res.userId == this.createUserId) {
          this.server.portrait = res.portrait;
          this.server.setuserPortrait(res.portrait);
          this.server.userportraiteventEmit.emit(true);
        }
        //昵称
        if (res.displayName && res.userId == this.createUserId) {
          this.server.username = res.displayName;
          this.server.setuserName(res.displayName);
          this.server.usernameeventEmit.emit(true);
        }
        $('.btn-warning').click();
        $('#update').modal('hide');

        //console.log(res)
        //prompt
        this.updateOutput.emit(res.result_code);
        //update
      }
    }).catch()

  }
}



